var imgkey;
BUI.use(['bui/uploader','bui/overlay'], function (Uploader,Overlay) {
	$(".todo,.finished").on("click",function(){
		$(".todo,.finished").addClass("todo");
		$(".todo,.finished").removeClass("finished");
		$(this).removeClass("todo");
		$(this).addClass("finished");
		var todo = $(this).attr("data-todo");
		var pickup = $(this).attr("data-pickup");
		var popup = $(this).attr("data-popup");
		var startup = $(this).attr("data-startup");
		$(".mob").hide();
		$(".pickup_band").hide();
		$(".popup_band").hide();
		$(".startup_band").hide();
		if(pickup==1){
			$(".pickup_band").show();
		}
		if(startup==1 ||todo=="band_gift"){
			$(".startup_band").show();
		}
		if(todo=="pickup"){
			$(".collect").show();
			return;
		}else{
			$("."+todo).show();
		}
		if(popup==1){
			$(".popup_band").show();
		}
		if(todo=="poupFadIn"){
			$(".poupFadIn").show();
		}else if(popup==1){
			$(".poupFadIn").show();
			$("#ruleBox,#rankBox,#awardBox").hide();
			switch(todo){
				case "notice":$(".slideBarTip").css("left",".6rem");$("#ruleBox").show();break;
				case "chance":$(".slideBarTip").css("left","5rem");$("#rankBox").show();break;
				case "gift":$(".slideBarTip").css("left","9.6rem");$("#awardBox").show();break;
			}
			$("."+todo).show();
		}
	});
	$(".tpl_set").on("mouseenter",function(){
		$(this).children(".tool").show();
		$(this).css({"border":"2px dashed #666","margin":"-2px"});
	});
	$(".tpl_set").on("mouseleave",function(){
		$(this).children(".tool").hide();
		$(this).css({"border":"none","margin":"0"});
	});
	$(".replac-music").on("click",function(){
		$(".control-group").removeClass("highlight");
		$(".music").addClass("highlight");
	});
	$(".front_gift").on("change",function(){
		var gid = $(this).val();//奖品ID
		var pos = $(this).attr("data-pos");//奖品所在位置
		var uppic = $(this).attr("data-uppic");//已上传图片的地址
		// if(!uppic){
			if(gid>0){ 
				//这个位置上有奖品，而且没有上传图片，则展示礼物箱
				$(".main_gift"+pos+"-val").val("https://img.alicdn.com/imgextra/i4/27390555/TB24G6eciRnpuFjSZFCXXX2DXXa-27390555.png");//哭脸
				$(".main_gift"+pos+"-show").attr("src","https://img.alicdn.com/imgextra/i4/27390555/TB24G6eciRnpuFjSZFCXXX2DXXa-27390555.png");
				$(".main_gift"+pos+"-divshow").css({"background":"url(https://img.alicdn.com/imgextra/i4/27390555/TB24G6eciRnpuFjSZFCXXX2DXXa-27390555.png) center no-repeat"});
			}else{
				//这个位置上没有奖品，展示哭脸
				$(".main_gift"+pos+"-val").val("https://img.alicdn.com/imgextra/i4/27390555/TB2sFC9cm4mpuFjSZFOXXaUqpXa-27390555.png");//哭脸
				$(".main_gift"+pos+"-show").attr("src","https://img.alicdn.com/imgextra/i4/27390555/TB2sFC9cm4mpuFjSZFOXXaUqpXa-27390555.png");
				$(".main_gift"+pos+"-divshow").css({"background":"url(https://img.alicdn.com/imgextra/i4/27390555/TB2sFC9cm4mpuFjSZFOXXaUqpXa-27390555.png) center no-repeat"});
			}
		// }
	});
	// var dialog = new Overlay.Dialog({
		// title:'元素替换',
		// width:400,
		// height:260,
		// mask:false,
		// buttons:[{
                // text:'保存',
                // elCls : 'button button-primary',
                // handler : function(){
                  // //do some thing
                  // this.close();
                // }
              // },{
                // text:'关闭',
                // elCls : 'button',
                // handler : function(){
                  // this.close();
                // }
              // }],
		// contentId:'dialog'
	// }); 
	$(".replac-bgpic").on("click",function(){
		var highlight = $(this).attr("data-highlight");
		$(".control-group").removeClass("highlight");
		$("."+highlight).addClass("highlight");
		// var pic = $(this).attr("data-init");
		// $(".show-pic").html("<img src=\""+pic+"_100x100\" />");
		// $(".show-pic-download").attr("href",pic);
        // dialog.show();
	});
	$(".J_Uploader").each(function(){
		var value = $(this).attr("data-val");
		var show = $(this).attr("data-show");
		var uploader = new Uploader.Uploader({
			// data:{"init":init},
			name: "userfile",
			render: this,
			url: '/services/img/apiupload/SaveImg.php',
			rules: {
			  //文的类型
			  ext: ['.png,.jpg,.gif','文件类型只能为{0}'],
			  //文件大小的最大值,单位也是kb
			  maxSize: [300, '文件大小不能大于300kb']
			}
		}).render();
		uploader.on('complete', function (ev) {
			var index = ev.index, file = ev.file;
			//服务器端返回的结果集
			var result = ev.result;
			$("."+value).val(result.url);
			$("."+show).attr("src",result.url+"_100x100");
			$("."+show).css("background-image","url("+result.url+")");
		});
		uploader.on('error', function (ev) {
			var index = ev.index, file = ev.file;
			//服务器端返回的结果集
			var result = ev.result;
			alert(result.msg);
		});
	});
	
	
	
	var addmp3 = new Uploader.Uploader({
        render: '.addmp3',
        url: '/?m=service&c=wantu&a=uploadmp3',
		rules: {
          //文的类型
          ext: ['.mp3','文件类型只能为{0}'],
          //文件大小的最大值,单位也是kb
          maxSize: [1024*3, '文件大小不能大于3M']
        }
	}).render();
	
	addmp3.on('complete', function (ev) {
		var index = ev.index, file = ev.file;
		//服务器端返回的结果集
		var result = ev.result;
		if(result.isSuccess){
			var aud = $(".audio").mb_miniPlayer({
				width: 240,
				inLine: false
			});
			aud.mb_miniPlayer_changeFile({"mp3":result.url},"上传的音乐");
			aud.mb_miniPlayer_play();
		}
	});
	addmp3.on('error', function (ev) {
		var index = ev.index, file = ev.file;
		//服务器端返回的结果集
		var result = ev.result;
		// alert(result.code);
	});
});


$(function() {
	var config = {
        color: "#ECC",
        flat: false,
        showInput: true,
        className: "full-spectrum",
        showInitial: true,
        showPalette: true,
        showSelectionPalette: true,
        maxPaletteSize: 10,
        preferredFormat: "hex",
        localStorageKey: "spectrum.example",
		cancelText: "关闭",
		chooseText: "设置",
        move: function (color) {
			color.toHexString(); // #ff0000
			// $(".show-pic").css({"background":color.toHexString()});
        },
        show: function () {

        },
        beforeShow: function () {

        },
        hide: function (color) {
        },

        palette: [
				["#000000", "#434343", "#666666", "#999999", "#b7b7b7", "#cccccc", "#d9d9d9", "#efefef", "#f3f3f3", "#ffffff"],
				["#980000", "#ff0000", "#ff9900", "#ffff00", "#00ff00", "#00ffff", "#4a86e8", "#0000ff", "#9900ff", "#ff00ff"],
				["#e6b8af", "#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d9ead3", "#c9daf8", "#cfe2f3", "#d9d2e9", "#ead1dc"],
				["#dd7e6b", "#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#a4c2f4", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
				["#cc4125", "#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6d9eeb", "#6fa8dc", "#8e7cc3", "#c27ba0"],
				["#a61c00", "#cc0000", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3c78d8", "#3d85c6", "#674ea7", "#a64d79"],
				["#85200c", "#990000", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#1155cc", "#0b5394", "#351c75", "#741b47"],
				["#5b0f00", "#660000", "#783f04", "#7f6000", "#274e13", "#0c343d", "#1c4587", "#073763", "#20124d", "#4c1130"]
			]
    };
	function setSingleColor(picker,value,target,cssname,cssval,cssval_add){
		config.move = function(color){
			$(target).css(cssname,cssval+color.toHexString()+cssval_add);
		};
		config.change = function(color){
			$(value).val(color.toHexString());
			$(target).css(cssname,cssval+color.toHexString()+cssval_add);
		};
		$(picker).spectrum(config);
	}
	//刮刮卡的辅助背景
	config.color = $(".main_numcolor-val").val(); //"#963";
	config.showAlpha = false;
	setSingleColor(".main_numcolor-picker",".main_numcolor-val",".gameInfoBox,.num","color","","");
	//刮刮卡的辅助背景
	config.color = $(".main_bgcolor-val").val(); //"#963";
	config.showAlpha = false;
	setSingleColor(".main_bgcolor-picker",".main_bgcolor-val",".gameInfoBox,.times","background","","");
	
	//以下是砸金蛋辅助
	config.color = $(".main_h2-val").val(); //"#963";
	config.showAlpha = false;
	setSingleColor(".main_h2-picker",".main_h2-val",".instro-wall h2","background","-webkit-linear-gradient(left,transparent 10%,"," 50%,transparent 90%)");
	
	config.color = $(".main_h2text-val").val(); //"#963";
	config.showAlpha = false;
	setSingleColor(".main_h2text-picker",".main_h2text-val",".instro-wall h2","color","","");
	
	config.color = $(".instro-wall-giftline-val").val(); //"#a17a5e";
	config.showAlpha = false;
	setSingleColor(".instro-wall-giftline",".instro-wall-giftline-val",".win-table tr,.win-table td","border-top","1px solid ","");
	
	config.color = $(".main_h6color-val").val(); //"#6d5939";
	config.showAlpha = false;
	setSingleColor(".main_h6color-picker",".main_h6color-val",".instro-wall article h6","color","","");
	
	config.color = $(".main_text-color-val").val(); //"#ffefd9";
	config.showAlpha = false;
	setSingleColor(".main_text-color-picker",".main_text-color-val",".instro-wall article .content,.instro-wall article table","color","","");
	
	//对话框边框色
	config.color = $(".dialog_border-val").val(); //"#393939";
	config.showAlpha = false;
	setSingleColor(".dialog_border-picker",".dialog_border-val",".ui-dialog_wrap","border","1px solid ","");
	
	//对话框字体色
	config.color = $(".dialog_text-val").val(); //"#a6a9ac";
	config.showAlpha = false;
	setSingleColor(".dialog_text-picker",".dialog_text-val",".ui-dialog_wrap","color","","");
	
	//按钮字体色
	config.color = $(".dialog_btntext-val").val();//"#a6a9ac"; 
	config.showAlpha = false;
	setSingleColor(".dialog_btntext-picker",".dialog_btntext-val","footer a","color","","");
	
	//锦囊按钮字体色
	config.color = $(".jinnang-button-text-val").val(); // "#FFF";
	config.showAlpha = false;
	setSingleColor(".jinnang-button-text-picker",".jinnang-button-text-val","#awardBox .codeInfoBox .buttons input,#rankBox #rankInfoBox .rankInfo input.mission","color","","");


	
	config.color = $(".instro-wall-article-val").val();//"rgba(144, 131, 123, 0.8)";
	config.showAlpha = true;
	config.move = function(color){
		$("article").css("webkit-box-shadow"," 0 0 15px "+color+" inset");
	};
	config.change = function(color){
		$(".instro-wall-article-val").val(color);
		$("article").css("webkit-box-shadow"," 0 0 15px "+color+" inset");
	};
	$(".instro-wall-article").spectrum(config);
	

	function setInstroWall(num){
		config.color = pickerSet[num];
		config.showAlpha = false;
		config.move = function(color){
			pickerSet[num] = color.toHexString();
			$(".instro-wall").css({"background-image":"-webkit-gradient(linear,10% 0,90% 0,from("+pickerSet[0]+"),color-stop(44.5%,"+pickerSet[1]+"),color-stop(56%,"+pickerSet[2]+"),color-stop(67.7%,"+pickerSet[3]+"),to("+pickerSet[4]+"))"});
		};
		config.change = function(color){
			pickerSet[num] = color.toHexString();
			$(".instro-wall-"+num+"val").val(pickerSet[num]);
			$(".instro-wall").css({"background-image":"-webkit-gradient(linear,10% 0,90% 0,from("+pickerSet[0]+"),color-stop(44.5%,"+pickerSet[1]+"),color-stop(56%,"+pickerSet[2]+"),color-stop(67.7%,"+pickerSet[3]+"),to("+pickerSet[4]+"))"});
		};
		$(".instro-wall-picker"+num).spectrum(config);	
	}
	var c0 = $(".instro-wall-0val").val();
	var c1 = $(".instro-wall-1val").val();
	var c2 = $(".instro-wall-2val").val();
	var c3 = $(".instro-wall-3val").val();
	var c4 = $(".instro-wall-4val").val();
	var pickerSet = [c0,c1,c2,c3,c4];//["#875e44","#b88e6f","#b7957a","#b99477","#875e44"];
	for(var i=0;i<pickerSet.length;i++){
		setInstroWall(i);
	}
	
	//对话框标题下划线
	var c0 = $(".dialog_hr0-val").val();
	var c1 = $(".dialog_hr1-val").val();
	var H2Hr = [c0,c1];//["#000","#414141"];
	for(var i=0;i<pickerSet.length;i++){
		setH2Hr(i);
	}
	function setH2Hr(num){
		config.color = H2Hr[num];
		config.showAlpha = false;
		config.move = function(color){
			H2Hr[num] = color.toHexString();
			$(".ui-dialog_wrap header h2 hr").css({"border-color":H2Hr[0]+" transparent "+H2Hr[1]});
		};
		config.change = function(color){
			H2Hr[num] = color.toHexString();
			$(".dialog_hr"+num+"-val").val(H2Hr[num]);
			$(".ui-dialog_wrap header h2 hr").css({"border-color":H2Hr[0]+" transparent "+H2Hr[1]});
		};
		$(".dialog_hr"+num+"-picker").spectrum(config);	
	}
	
	//对话框背景渐变	
	var c0 = $(".dialog_bg0-val").val();
	var c1 = $(".dialog_bg1-val").val();
	var colors = [c0,c1]; //["#000","#414141"];
	for(var i=0;i<pickerSet.length;i++){
		setDialogBg(colors,".dialog_bg",".ui-dialog_wrap",i);
	}
	//关闭按钮渐变
	var c0 = $(".dialog_btnclose0-val").val();
	var c1 = $(".dialog_btnclose1-val").val();
	var colors = [c0,c1]; // ["#454c54","#30353b"];
	for(var i=0;i<pickerSet.length;i++){
		setDialogBg(colors,".dialog_btnclose",".ui-dialog_wrap footer .btn-no",i);
	}
	//ok按钮背景渐变
	var c0 = $(".dialog_btnok0-val").val();
	var c1 = $(".dialog_btnok1-val").val();
	var colors = [c0,c1];//["#308ce8","#2262a3"];
	for(var i=0;i<pickerSet.length;i++){
		setDialogBg(colors,".dialog_btnok",".ui-dialog_wrap footer .btn-yes",i);
	}
	//奖品按钮背景
	var c0 = $(".instro-wall-gift-button0-val").val();
	var c1 = $(".instro-wall-gift-button1-val").val();
	var buttonColor = [c0,c1];//["#1dbbd7","#10369d"];
	for(var i=0;i<buttonColor.length;i++){
		setDialogBg(buttonColor,".instro-wall-gift-button",".instro-wall article table td input",i);
	}
	//锦囊按钮背景
	var c0 = $(".jinnang-button0-val").val();
	var c1 = $(".jinnang-button1-val").val();
	var buttonColor = [c0,c1];//["#1dbbd7","#10369d"];
	for(var i=0;i<buttonColor.length;i++){
		setDialogBg(buttonColor,".jinnang-button","#awardBox .codeInfoBox .buttons input,#rankBox #rankInfoBox .rankInfo input.mission",i);
	}
	
	function setDialogBg(myColor,picker,target,num){
		config.color = myColor[num];
		config.showAlpha = false;
		config.move = function(color){
			myColor[num] = color.toHexString();
			$(target).css({"background":"-webkit-linear-gradient(top,"+myColor[0]+","+myColor[1]+")"});
		};
		config.change = function(color){
			myColor[num] = color.toHexString();
			$(picker+num+"-val").val(myColor[num]);
			$(target).css({"background":"-webkit-linear-gradient(top,"+myColor[0]+","+myColor[1]+")"});
		};
		$(picker+num+"-picker").spectrum(config);	
	}
	
	
	$(".js-notice_auto").on("click",function(){$(".notice_input").hide()});
	$(".js-notice_input").on("click",function(){$(".notice_input").show()});
	$("input[type=radio]").on("click",function(){
		var v = $(this).val();
		var tar = $(this).attr("data-target");
		if(v==2){
			$("."+tar).hide();
		}else{
			$("."+tar).show();
		}
	});
	
});